﻿Public Class ADDPRODUCT
    Dim connect As CONDB = CONDB.NewConnection()
    Dim s As FILTERCLASS
    Private Sub ADDPRODUCT_Load(sender As Object, e As EventArgs) Handles Me.Load
        SelectDatagridMasProduct()
        SearchPRODUCTTYPE()
        SelectDatagridMasProduct2()
    End Sub
    Private Sub SelectDatagridMasProduct()
        Dim objSelectDataProduct As New MASPRODUCT2
        Dim Product As DataTable
        Product = connect.GetTable(objSelectDataProduct.SelectMasproduct_)
        Grid1.PrimaryGrid.DataSource = Product
    End Sub
    Private Sub SelectDatagridMasProduct2()
        Dim objSelectDataProduct As New MASPRODUCT
        Dim Product As DataTable
        Product = connect.GetTable(objSelectDataProduct.SelectProductType_)
        Grid2.PrimaryGrid.DataSource = Product
    End Sub
    Private Sub SearchPRODUCTTYPE()
        Dim sql2 As String
        sql2 = "SELECT CAST(idmasproducttype AS CHAR(10)) AS `idmasproducttype`,`typename` FROM cpm.masproducttype WHERE `status` = 1 "
        s = New FILTERCLASS(PRODUCTTYPE2Txt, sql2, "รหัสประเภท,ประเภท", "50,200", "1,1", "1,1")
        s.SetShowBorder = True
    End Sub

    Private Sub AddProtype_Click(sender As Object, e As EventArgs) Handles AddProtype.Click
        If PRODUCTTYPETxt.Text.ToString.Length > 0 Then
            Dim objInsertData As New MASPRODUCT
            objInsertData.TYPENAME_ = PRODUCTTYPETxt.Text
            objInsertData.insertMasProducttype()

            SelectDatagridMasProduct2()

            cleardata()
            s.SetSql = "SELECT CAST(idmasproducttype AS CHAR(10)) AS `idmasproducttype`,`typename` FROM cpm.masproducttype WHERE `status` = 1"
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
    End Sub
    Private Sub EditProtype_Click(sender As Object, e As EventArgs) Handles EditProtype.Click
        If PRODUCTTYPEIDTxt.Text.ToString.Length > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            If editStat = 1 Then
                Dim objeditData As New MASPRODUCT
                objeditData.IDMASPRODUCTTYPE_ = PRODUCTTYPEIDTxt.Text
                objeditData.TYPENAME_ = PRODUCTTYPETxt.Text
                objeditData.editMasProducttype()

                SelectDatagridMasProduct2()

                cleardata()
            End If
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
        AddProtype.Enabled = True
    End Sub
    Private Sub cleardata()
        SelectDatagridMasProduct()
        SelectDatagridMasProduct2()
        PRODUCTTxt.Text = ""
        PRODUCTTYPE2Txt.Text = ""
        PRODUCTTYPETxt.Text = ""
        PRODUCTTYPEIDTxt.Text = ""
        BPRICETxt.Text = ""
        SPRICETxt.Text = ""
        QTYTxt.Text = ""
        ID.Text = ""
        REMARKRTxt.Text = ""
        QTY2.Text = ""
        QTY3.Text = ""
    End Sub

    Private Sub Grid2_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles Grid2.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            PRODUCTTYPEIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            PRODUCTTYPEIDTxt.Text = ""
        End If
        If grid.Cells("ประเภทสินค้า").Value() IsNot DBNull.Value Then
            PRODUCTTYPETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเภทสินค้า").Value()
        Else
            PRODUCTTYPETxt.Text = ""
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masproducttype SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `idmasproducttype` = '" & PRODUCTTYPEIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        AddProtype.Enabled = False
    End Sub

    Private Sub AddProd_Click(sender As Object, e As EventArgs) Handles AddProd.Click
        If PRODUCTTxt.Text.ToString.Length > 0 And txtUsername.Text.Length > 0 Then
            Dim objInsertData As New MASPRODUCT2
            objInsertData.PRODUCTNAME_ = PRODUCTTxt.Text
            objInsertData.IDMASPRODUCTTYPE_ = Convert.ToString(PRODUCTTYPE2Txt.Tag).Trim
            objInsertData.BPRICE_ = BPRICETxt.Text
            objInsertData.SPRICE_ = SPRICETxt.Text
            objInsertData.QTY_ = QTYTxt.Text
            objInsertData.REMARK_ = REMARKRTxt.Text
            objInsertData.USREQ_ = USREQ.Text
            objInsertData.CODE_ = CODETxt.Text
            objInsertData.insertMasproduct2()

            'Dim sql As String
            'sql = "INSERT INTO cpm.hisbuy (`product`,`qty`,`usreq`,date) VALUES ('" & PRODUCTTxt.Text & "','" & QTYTxt.Text & "','" & USREQ.Text & "',current_timestamp); "
            'connect.ExecuteNonQuery(sql)

            SelectDatagridMasProduct()

            cleardata()


        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub EditProd_Click(sender As Object, e As EventArgs) Handles EditProd.Click
        If ID.Text.ToString.Length > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            If editStat = 1 Then
                Dim objeditData As New MASPRODUCT2
                objeditData.IDMASPRODUCTTYPE_ = Convert.ToString(PRODUCTTYPE2Txt.Tag).Trim
                objeditData.PRODUCTNAME_ = PRODUCTTxt.Text
                objeditData.IDMASPRODUCT_ = ID.Text
                objeditData.BPRICE_ = BPRICETxt.Text
                objeditData.SPRICE_ = SPRICETxt.Text
                objeditData.QTY_ = QTY3.Text
                objeditData.editMasproduct2()

                Dim sql As String
                sql = "INSERT INTO cpm.hisbuy (`product`,`qty`,`usreq`,`date`,`bprice`) VALUES ('" & ID.Text & "','" & QTY2.Text & "','" & USREQ.Text & "',current_timestamp,'" & BPRICETxt.Text & "'); "
                connect.ExecuteNonQuery(Sql)

                SelectDatagridMasProduct()

                cleardata()
            End If
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
        AddProd.Enabled = True
    End Sub

    Private Sub Grid1_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles Grid1.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            ID.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            ID.Text = ""
        End If
        If grid.Cells("สินค้า").Value() IsNot DBNull.Value Then
            PRODUCTTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สินค้า").Value()
        Else
            PRODUCTTxt.Text = ""
        End If
        If grid.Cells("idmasproducttype").Value() IsNot DBNull.Value Then
            PRODUCTTYPE2Txt.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("idmasproducttype").Value()
        Else
            PRODUCTTYPE2Txt.Tag = ""
        End If
        If grid.Cells("ประเภทสินค้า").Value() IsNot DBNull.Value Then
            PRODUCTTYPE2Txt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเภทสินค้า").Value()
        Else
            PRODUCTTYPE2Txt.Text = ""
        End If
        If grid.Cells("จำนวน").Value() IsNot DBNull.Value Then
            QTYTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จำนวน").Value()
        Else
            QTYTxt.Text = ""
        End If
        If grid.Cells("ราคาซื้อ").Value() IsNot DBNull.Value Then
            BPRICETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคาซื้อ").Value()
        Else
            BPRICETxt.Text = ""
        End If
        If grid.Cells("ราคาขาย").Value() IsNot DBNull.Value Then
            SPRICETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคาขาย").Value()
        Else
            SPRICETxt.Text = ""
        End If
        If grid.Cells("remark").Value() IsNot DBNull.Value Then
            REMARKRTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("remark").Value()
        Else
            REMARKRTxt.Text = ""
        End If
        If grid.Cells("CODE").Value() IsNot DBNull.Value Then
            CODETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODE").Value()
        Else
            CODETxt.Text = ""
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE cpm.masproduct SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `idmasproduct` = '" & ID.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        QTY2.Enabled = True
        AddProd.Enabled = False
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles btnclear.Click
        SelectDatagridMasProduct()
        SelectDatagridMasProduct2()
        PRODUCTTxt.Text = ""
        PRODUCTTYPE2Txt.Text = ""
        PRODUCTTYPETxt.Text = ""
        PRODUCTTYPEIDTxt.Text = ""
        BPRICETxt.Text = ""
        SPRICETxt.Text = ""
        QTYTxt.Text = ""
        ID.Text = ""
        AddProd.Enabled = True
        REMARKRTxt.Text = ""
    End Sub

    Private Sub txtUserReq_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtUserReq.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            Dim dt As DataTable
            Dim sql As String
            If connect.GetTable("SELECT `usreq` FROM cpm.masuser WHERE `usreq` = '" & txtUserReq.Text & "' ").Rows.Count > 0 Then
                sql = "SELECT `userid`,`name`,`lastname`,`status`,`usreq` FROM cpm.masuser WHERE `usreq` = '" & txtUserReq.Text & "'; "
                dt = connect.GetTable(sql)
                If dt.Rows.Count > 0 Then
                    txtUsername.Text = dt.Rows(0)("name") & "  " & dt.Rows(0)("lastname")
                    USREQ.Text = dt.Rows(0)("userid")
                End If
                btnLogout.Visible = True
                Grid1.Enabled = True
                Grid2.Enabled = True
                AddProd.Enabled = True
                AddProtype.Enabled = True
                EditProd.Enabled = True
                EditProtype.Enabled = True
                btnclear.Enabled = True
                btnnextform.Enabled = True
                GroupBox1.Enabled = True
                GroupBox2.Enabled = True
            Else
                MsgBox("รหัสไม่ถูกต้อง", MsgBoxStyle.Critical)
                txtUsername.Text = ""
                txtUserReq.Text = ""
            End If
        End If
    End Sub

    Private Sub btnnextform_Click(sender As Object, e As EventArgs) Handles btnnextform.Click
        Dim nextform As New HISBUYPRODUCT
        nextform.ShowDialog()
    End Sub

    Private Sub QTY2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles QTY2.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(13) Then
            Dim a As Integer
            a = QTYTxt.Text
            Dim b As Integer
            If QTY2.Text = "" Then
                MsgBox("กรุณากรอกจำนวน")
                Return
            End If
            b = QTY2.Text
           
            'Dim c As Integer
            'c = QTY3.Text
            Try

                QTY3.Text = a + b
            Catch ex As Exception

            End Try
        End If
    End Sub
End Class
